package cn.crm.system.service.impl;

import cn.crm.common.annotation.DataScope;
import cn.crm.common.utils.SecurityUtils;
import cn.crm.system.domain.pojo.dto.*;
import cn.crm.system.domain.pojo.entity.Business;
import cn.crm.system.domain.pojo.vo.BusinessVo;
import cn.crm.system.domain.pojo.vo.CrmspecificationpictureVo;
import cn.crm.system.domain.pojo.vo.ProdectVo;
import cn.crm.system.domain.pojo.vo.SpecificationVo;
import cn.crm.system.mapper.*;
import cn.crm.system.service.IBusinessService;
import cn.crm.system.service.IProdectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 处理客户的接口
 */

@Slf4j
@Service
public class ProdectServiceImpl implements IProdectService {

    @Autowired
    private ProdectMapper prodectMapper;
    @Autowired
    private CrmspecificationpictureMapper crmspecificationpictureMapper;
    @Autowired
    private SpecificationMapper specificationMapper;

    @Override
    public int insertProdect(ProdectDto prodectDto) {
       //开始新增商品
        //        获取到当前用户
        String createAccount = SecurityUtils.getUsername();
        //赋值给业务表里
        prodectDto.setCreateBy(createAccount);
        int i = prodectMapper.insertProdect(prodectDto);

        Long specificationId = prodectDto.getId();
        if (specificationId != null) {
            List<CrmspecificationpictureDto> crmspecificationpictureList = prodectDto.getCrmspecificationpicture();
            crmspecificationpictureList.forEach(CrmspecificationpictureDto -> {
                CrmspecificationpictureDto.setSpecificationId(specificationId);
                CrmspecificationpictureDto.setCreateBy(createAccount);
            });
            crmspecificationpictureMapper.insertCrmpicture(crmspecificationpictureList);
        }

        return i;
    }
//查询商品列表
    @Override
    public List<ProdectVo> selectProdect(ProdectDto prodectDto) {
        //开始查询商品列表
        List<ProdectVo> prodectVos = prodectMapper.selectProdect(prodectDto);
        return prodectVos;
    }
    //删除商品
    @Override
    public int deletespecification(Long[] id, Long[] specificationId) {
        int j =0;
        int k =0;
        int m =0;
        int row = crmspecificationpictureMapper.selectcrmpicturebyidInt(specificationId);
        //如果就一笔就删除图片数据和表头
        if (row ==1 || row ==0){
            //删除表身资料
            j = prodectMapper.deletespecification(id);
            //删除对应的图片
            k  = crmspecificationpictureMapper.delCrmpicture(specificationId);
            //删除表单资料
            m = specificationMapper.deletespecificationByprodectId(id);
        }else {
            //否则只删除图片的数据
            k  = crmspecificationpictureMapper.delCrmpicture(specificationId);

            int rows = crmspecificationpictureMapper.selectcrmpicturebyidInt(id);
            if (rows==0){
                //删除表头数据
                j= prodectMapper.deletespecification(id);
                m = specificationMapper.deletespecificationByprodectId(id);
            }
        }

        return  j+k+m;
    }
    //通过id查询商品资料
    @Override
    public ProdectVo selectProdectById(Long id) {
        ProdectVo prodectVo = prodectMapper.selectProdectById(id);
        //查询图片的
        List<CrmspecificationpictureVo> selectcrmpicturebyid = crmspecificationpictureMapper.selectcrmpicturebyid(id);
        prodectVo.setCrmspecificationpicture(selectcrmpicturebyid);
        return prodectVo;
    }
//修改商品
    @Override
    public int updateProdect(ProdectDto prodectDto) {
        //开始修改商品
        int i = prodectMapper.updatespecification(prodectDto);
        int i1=0;
        //修改图片
        List<CrmspecificationpictureDto> crmspecificationpicture = prodectDto.getCrmspecificationpicture();
        // 分离出需要更新和新增的记录
        List<CrmspecificationpictureDto> toUpdate = crmspecificationpicture.stream()
                .filter(item -> item.getId() != null && item.getId() != 0)
                .collect(Collectors.toList());

        List<CrmspecificationpictureDto> toInsert = crmspecificationpicture.stream()
                .filter(item -> item.getId() == null || item.getId() == 0)
                .collect(Collectors.toList());
        if (toUpdate.size() > 0 ){
            i1 = crmspecificationpictureMapper.updateCrmpicture(toUpdate);
        }
        // 新增操作
        if (!toInsert.isEmpty()) {
            // 获取当前登录用户
            String createAccount = SecurityUtils.getUsername();

            // 设置每个对象的 specificationId 和 createBy
            toInsert.forEach(item -> {
                item.setSpecificationId(prodectDto.getId());
                item.setCreateBy(createAccount);
            });

            // 执行插入操作
            i1 += crmspecificationpictureMapper.insertCrmpicture(toInsert);
        }
        if (i+i1<1){
            return 0;
        }
        return i+i1;
    }
//查询出4笔数据
    @Override
    public List<ProdectVo> selectprodectFour() {
        List<ProdectVo> prodectVos = prodectMapper.selectprodectFour();
        return prodectVos;
    }

    @Override
    public ProdectVo selectProdectAllById(Long id) {
        //根据商品id查询出系列和规格
        ProdectVo prodectVo = prodectMapper.selectProdectOnly(id);
        //然后在通过商品id 去查参数
        List<SpecificationVo> specificationVos = specificationMapper.selectSpecificationByProdectid(id);
        prodectVo.setSpecificationVos(specificationVos);
        return prodectVo;
    }
}
